
//获取随机数
function getRandNum() {
    // console.log('getRandNum()');
    var num = Math.floor(52 * Math.random());
    return (num);
}
//是否存在
function ifhava(rnumb) {
    // console.log('ifhava()');
    for (i = 0; i <= 51; i++) {
        if (all_pokerIndex[i] == rnumb) {
            //存在
            return false;
        } else if (all_pokerIndex[i] == undefined) {
            return true;
        }
    }
    return true;
}
// 存储随机数
function setNumber() {
    //    console.log('setNumber()');
    for (k = 0; k <= 51; k++) {
        for (n = 0; n <= 9999; n++) {
            var rand = getRandNum()
            //console.log(rand)
            var ifh = ifhava(rand)
            if (ifh) {
                all_pokerIndex[k] = rand;
                break;
                //console.log('allpoker['+k+']=:'+rand)
            }
        }
    }
    //console.log('end')
}
//随机牌产生
function sendpoker(indexs, pages) {
    // console.log('sendpoker()');
    var thetime = 0;
    var t = setInterval(() => {
        $('.line_li').eq(thetime).append(all_poker[all_pokerIndex[indexs]]);
        // console.log(all_poker[all_pokerIndex[indexs]])
        indexs++;
        thetime++;
        if (thetime > 10) {
            clearInterval(t)
        }
    }, 150);
}
//发牌
function outPokers() {
    // console.log('outPokers');
    switch (sendtime) {
        case 0: {
            sendpoker(0, 9)
            $('.bottom_font').html('5');
            break;
        }
        case 1: {
            sendpoker(10, 19)
            $('.bottom_font').html('4');
            break;
        }
        case 2: {
            sendpoker(20, 29)
            $('.bottom_font').html('3');
            break;
        }
        case 3: {
            sendpoker(30, 39)
            $('.bottom_font').html('2');
            break;
        }
        case 4: {
            sendpoker(40, 49)
            $('.bottom_font').html('1');
            break;
        }
        case 5: {
            sendpoker(50, 51)
            $('.bottom_font').html('');
            $('.bottom_font').addClass('bottom2').removeClass('bottom_font');
            break;
        }
        default: {
            break;
        }
    }
    sendtime++;
}

//拖拽
var objts;
var ss = 0;
function menter(objt, event) {
    // console.log('menter()');
    var ifhas = $(objt).hasClass('canmove')
    if (ifhas) {
        var isMove = true;
        var eventLine_eq;
        eventLine_eq = getline_li_XY(event.pageX);
        var now_length=$('.line_li').eq(eventLine_eq).children().length;//5
        var now_index=$('.line_li').eq(eventLine_eq).find($(objt)).index() //2

        if(now_length==now_index+1){
            //一张
            var abs_x = event.pageX - $(objt).offset().left;
            var abs_y = event.pageY - $(objt).offset().top;
            var eventLine_old = getline_li_XY(event.pageX);
            $(this).mousemove(function (event) {
                if (isMove) {
                    $(objt).css({
                        'zIndex': 10,
                        'left': event.pageX - abs_x,
                        'top': event.pageY - abs_y
                    });
                    eventLine_eq = getline_li_XY(event.pageX);
                    $('.line_li').eq(eventLine_eq).addClass('line_li_move').siblings().removeClass('line_li_move');
                }
            })
            $(this).mouseup(function () {
                isMove = false;
            })
        }else{
            //多张
                var abs_x = event.pageX - $(objt).offset().left;
                var abs_y = event.pageY - $(objt).offset().top;
                $(this).mousemove(function (event) {
                    if (isMove) {
                            $('.line_li').eq(eventLine_eq).children().eq(now_index).css({
                                'zIndex': 10,
                                'left': event.pageX - abs_x,
                                'top': event.pageY - abs_y
                            }); 

                            if(now_length-now_index>=1){
                                $('.line_li').eq(eventLine_eq).children().eq(now_index+1).css({
                                    'zIndex': 10,
                                    'left': event.pageX - abs_x,
                                    'top': event.pageY - abs_y + 35
                                });
                            }
                            if(now_length-now_index>=2){
                                $('.line_li').eq(eventLine_eq).children().eq(now_index+2).css({
                                    'zIndex': 10,
                                    'left': event.pageX - abs_x,
                                    'top': event.pageY - abs_y + 70
                                });
                            }
                            if(now_length-now_index>=3){
                                $('.line_li').eq(eventLine_eq).children().eq(now_index+3).css({
                                    'zIndex': 10,
                                    'left': event.pageX - abs_x,
                                    'top': event.pageY - abs_y + 3*35
                                });
                            }
                            if(now_length-now_index>=4){
                                $('.line_li').eq(eventLine_eq).children().eq(now_index+4).css({
                                    'zIndex': 10,
                                    'left': event.pageX - abs_x,
                                    'top': event.pageY - abs_y + 4*35
                                });
                            }
                            if(now_length-now_index>=5){
                                $('.line_li').eq(eventLine_eq).children().eq(now_index+5).css({
                                    'zIndex': 10,
                                    'left': event.pageX - abs_x,
                                    'top': event.pageY - abs_y + 5*35
                                });
                            }
                            if(now_length-now_index>=6){
                                $('.line_li').eq(eventLine_eq).children().eq(now_index+6).css({
                                    'zIndex': 10,
                                    'left': event.pageX - abs_x,
                                    'top': event.pageY - abs_y + 6*35
                                });
                            }
                            if(now_length-now_index>=7){
                                $('.line_li').eq(eventLine_eq).children().eq(now_index+7).css({
                                    'zIndex': 10,
                                    'left': event.pageX - abs_x,
                                    'top': event.pageY - abs_y + 7*35
                                });
                            }
                            if(now_length-now_index>=8){
                                $('.line_li').eq(eventLine_eq).children().eq(now_index+8).css({
                                    'zIndex': 10,
                                    'left': event.pageX - abs_x,
                                    'top': event.pageY - abs_y + 8*35
                                });
                            }
                            if(now_length-now_index>=9){
                                $('.line_li').eq(eventLine_eq).children().eq(now_index+9).css({
                                    'zIndex': 10,
                                    'left': event.pageX - abs_x,
                                    'top': event.pageY - abs_y + 9*35
                                });
                            }
                            if(now_length-now_index>=10){
                                $('.line_li').eq(eventLine_eq).children().eq(now_index+10).css({
                                    'zIndex': 10,
                                    'left': event.pageX - abs_x,
                                    'top': event.pageY - abs_y + 10*35
                                });
                            }
                            if(now_length-now_index>=11){
                                $('.line_li').eq(eventLine_eq).children().eq(now_index+11).css({
                                    'zIndex': 10,
                                    'left': event.pageX - abs_x,
                                    'top': event.pageY - abs_y + 11*35
                                });
                            }
                            if(now_length-now_index>=12){
                                $('.line_li').eq(eventLine_eq).children().eq(now_index+12).css({
                                    'zIndex': 10,
                                    'left': event.pageX - abs_x,
                                    'top': event.pageY - abs_y + 12*35
                                });
                            }
                            $('.line_li').eq(getline_li_XY(event.pageX)).addClass('line_li_move').siblings().removeClass('line_li_move');
                        }
                })
                $(document).mouseup(function () {
                    $('.line_li').removeClass('line_li_move')
                    isMove = false;
                })
        } 
    }
}
function mup(mup_this, event) {
    // console.log('mup()');
    var eventLine_eq = getline_li_XY(event.pageX);
    var ifhas = $(mup_this).hasClass('canmove')
    if (ifhas) {
        var l_lod=findpoker(mup_this)
        var now_length=$('.line_li').eq(l_lod).children().length;//5
        var now_index=$('.line_li').eq(l_lod).find($(mup_this)).index() //2
        if(now_length==now_index+1){
            //单张
            isMove = false;
            if (getline_li_XY(event.pageX) == 10) {
                //在空处
            } else {
                //牌交换
                poker_transfer(findpoker(mup_this), mup_this, getline_li_XY(event.pageX))
                //xy位置
                correctPokerXY(getline_li_XY(event.pageX), mup_this)
                canmoves()
            }
        }else{
            if (getline_li_XY(event.pageX) == 10) {
                //在空处
            } else {
                // 牌交换
                if(l_lod==getline_li_XY(event.pageX)){
                    for(var xi=0;xi<now_length;xi++){
                        var bottompoker=$('.line_li').eq(l_lod).children().eq(xi)
                        correctPokerXY(l_lod,bottompoker)
                    }
                    correctPokerXY(getline_li_XY(event.pageX, bottompoker))
                }else{
                    poker_transfer(findpoker(mup_this), mup_this, getline_li_XY(event.pageX))
                    correctPokerXY(getline_li_XY(event.pageX), mup_this)
                    //底下牌跟随
                    for(var a=1;a<now_length-now_index;a++){
                        var bottompoker=$('.line_li').eq(l_lod).children().eq(now_index)
                        poker_transfer(l_lod,bottompoker,findpoker(mup_this))
                        correctPokerXY(getline_li_XY(event.pageX), bottompoker)
                        canmoves()
                        $('.line_li').eq(l_lod).removeClass('line_li_move')
                }
                
                }
                //xy位置
                canmoves()
            }
        }
    }
}
//寻找牌 
function findpoker(poker_ojb) {
    // console.log('findpoker()');
    for (var i = 0; i <= 9; i++) {
        var l = $('.line_li').eq(i).find(poker_ojb).length
        if (l != 0) {
            return i;
            break;
        }
    }
}
//lineli中牌转移
function poker_transfer(_old, _objt, _eq) {
    // console.log('poker_transfer()');
    if (_old != _eq) {
        //删除原框内牌
        $('.line_li').eq(_old).find($(_objt)).remove()
        if (_eq == undefined) {
            $('.line_li').eq(_old).append($(_objt))
        } else {
            //新框内append
            $('.line_li').eq(_eq).append($(_objt))
        }
    }else{
    }
    $('.line_li').removeClass('line_li_move')
}
//修正牌坐标
function correctPokerXY(_nowlieq, _objt) {
    // console.log('correctPokerXY()');
    var l = $('.line_li').eq(_nowlieq).find($(_objt)).index();
    var left = $('.line_li').eq(_nowlieq).offset().left
    $('.line_li').eq(_nowlieq).find($(_objt)).css({
        'z-index': 9,
        'top': 50 + ((l) * 35),
        'left': left
    })
}
//最后一张canmove
function canmoves() {
    // console.log('canmoves()');
    var numbottom = 0;
    for (var i = 0; i <= 9; i++) {
        //最后一个元素
        var line_li_length = $('.line_li').eq(i).children().length - 1;
        if (line_li_length == 0) {
            // 数量为 1
            $('.line_li').eq(i).children('div:first-child').addClass('canmove')
        } else {
            var k = line_li_length;
            for (k; k >= 0; k--) {
                $('.line_li').eq(i).children().eq(k).removeClass('canmove')
                $('.line_li').eq(i).children().eq(line_li_length).addClass('canmove')
            };
            sum = 0;
            var count = line_li_length;
            while (count >= 0) {
                var nowindex1 = getNum_poker($('.line_li').eq(i).children().eq(count).html());
                var nowindex2 = getNum_poker($('.line_li').eq(i).children().eq(count - 1).html());
                if (nowindex1 == (nowindex2 - 1)) {
                    $('.line_li').eq(i).children().eq(count).addClass('canmove');
                    $('.line_li').eq(i).children().eq(count - 1).addClass('canmove');
                    sum++;
                    if (sum == 12) {
                        oversum++;
                       var eq=i;
                       var q=0;
                       var t_l=$('.line_li').eq(eq).children().length - 1;
                       var t_l2=t_l-13;
                       var ref = setInterval(function(){
                        var pk=$('.line_li').eq(eq).children().eq(t_l);
                        $(pk).addClass('poker overpoker').removeClass('ht hx mh fk add_poker canmove').html('');
                        $('#getbox').append($(pk));
                        t_l--;
                        if(t_l==t_l2){
                            clearInterval(ref)
                        }
                        },100);
                        setTimeout(() => {
                            if(oversum==5){
                                alert('over')
                            }
                        }, 2000);
                        
                    }
                } else {
                    break;
                }
                count--;
                if (count == -1) {
                    break;
                }
            }
        }
    }
}
//get poker num
function getNum_poker(num) {
    // console.log('getNum_poker()');
    for (var i = 0; i <= 12; i++) {
        if (poker_index[i] == num) {
            return i;
        }
    }
}
// 获取line_li坐标
function getline_li_XY(Xnum) {
    // console.log('getline_li_xy()');
    if (Xnum > ($('.line_li').eq(0).offset().left - 10) && Xnum < ($('.line_li').eq(0).offset().left + 114)) {
        return 0;
    } else if (Xnum > ($('.line_li').eq(1).offset().left - 10) && Xnum < ($('.line_li').eq(1).offset().left + 114)) {
        return 1;
    } else if (Xnum > ($('.line_li').eq(2).offset().left - 10) && Xnum < ($('.line_li').eq(2).offset().left + 114)) {
        return 2;
    } else if (Xnum > ($('.line_li').eq(3).offset().left - 10) && Xnum < ($('.line_li').eq(3).offset().left + 114)) {
        return 3;
    } else if (Xnum > ($('.line_li').eq(4).offset().left - 10) && Xnum < ($('.line_li').eq(4).offset().left + 114)) {
        return 4;
    } else if (Xnum > ($('.line_li').eq(5).offset().left - 10) && Xnum < ($('.line_li').eq(5).offset().left + 114)) {
        return 5;
    } else if (Xnum > ($('.line_li').eq(6).offset().left - 10) && Xnum < ($('.line_li').eq(6).offset().left + 114)) {
        return 6;
    } else if (Xnum > ($('.line_li').eq(7).offset().left - 10) && Xnum < ($('.line_li').eq(7).offset().left + 114)) {
        return 7;
    } else if (Xnum > ($('.line_li').eq(8).offset().left - 10) && Xnum < ($('.line_li').eq(8).offset().left + 114)) {
        return 8;
    } else if (Xnum > ($('.line_li').eq(9).offset().left - 10) && Xnum < ($('.line_li').eq(9).offset().left + 114)) {
        return 9;
    } else {
        return 10;
    }
}
//over
//数值定义

var oversum=0;
//senpoker clik time
var sendtime = 0;
//poker‘s index
var all_pokerIndex = new Array(52);
//all_poker
var all_poker = [
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='ht add_poker canmove'>A</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='hx add_poker canmove'>A</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='fk add_poker canmove'>A</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='mh add_poker canmove'>A</div>",

    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='ht add_poker canmove'>2</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='hx add_poker canmove'>2</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='fk add_poker canmove'>2</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='mh add_poker canmove'>2</div>",

    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='ht add_poker canmove'>3</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='hx add_poker canmove'>3</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='fk add_poker canmove'>3</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='mh add_poker canmove'>3</div>",

    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='ht add_poker canmove'>4</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='hx add_poker canmove'>4</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='fk add_poker canmove'>4</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='mh add_poker canmove'>4</div>",

    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='ht add_poker canmove'>5</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='hx add_poker canmove'>5</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='fk add_poker canmove'>5</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='mh add_poker canmove'>5</div>",

    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='ht add_poker canmove'>6</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='hx add_poker canmove'>6</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='fk add_poker canmove'>6</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='mh add_poker canmove'>6</div>",

    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='ht add_poker canmove'>7</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='hx add_poker canmove'>7</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='fk add_poker canmove'>7</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='mh add_poker canmove'>7</div>",

    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='ht add_poker canmove'>8</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='hx add_poker canmove'>8</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='fk add_poker canmove'>8</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='mh add_poker canmove'>8</div>",

    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='ht add_poker canmove'>9</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='hx add_poker canmove'>9</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='fk add_poker canmove'>9</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='mh add_poker canmove'>9</div>",

    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='ht add_poker canmove'>10</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='hx add_poker canmove'>10</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='fk add_poker canmove'>10</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='mh add_poker canmove'>10</div>",

    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='ht add_poker canmove'>J</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='hx add_poker canmove'>J</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='fk add_poker canmove'>J</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='mh add_poker canmove'>J</div>",

    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='ht add_poker canmove'>Q</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='hx add_poker canmove'>Q</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='fk add_poker canmove'>Q</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='mh add_poker canmove'>Q</div>",

    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='ht add_poker canmove'>K</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='hx add_poker canmove'>K</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='fk add_poker canmove'>K</div>",
    "<div onmousedown='menter(this,event)' onmouseup='mup(this,event)' class='mh add_poker canmove'>K</div>",
];
var poker_index = [
    "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
];
